﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Yuebon.Car.Application;
using Yuebon.Car.Dtos;
using Yuebon.Car.IServices;
using Yuebon.Car.Models;
using Yuebon.Commons;
using Yuebon.Commons.Helpers;
using Yuebon.Commons.Models;
using Yuebon.Shop.Application;
using Yuebon.Shop.Dtos;
using Yuebon.WebApi.Areas.Car.Models;
using Yuebon.WebApi.Common;
using Yuebon.WebApi.Controllers;
using Yuebon.WebApi.Models;

namespace Yuebon.WebApi.Areas.Car.Controllers
{
    /// <summary>
    /// 汽车车型
    /// </summary>
    [Route("api/Car/[controller]")]
    [ApiController]
    public class AutoDetailsController : BusinessApiController<AutoDetails, IAutoDetailsService>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_iService"></param>
        public AutoDetailsController(IAutoDetailsService _iService) : base(_iService)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        protected override void OnBeforeInsert(AutoDetails info)
        {
            info.Id = GuidUtils.CreateNo();
            info.CreatorTime = DateTime.Now;
            info.CreatorUserId = CurrentUser.Id;
            info.DeleteMark = false;
            info.CompanyId = CurrentUser.OrganizeId;
            info.DeptId = CurrentUser.DepartmentId;
            if (info.SortCode == null)
            {
                info.SortCode = 99;
            }
        }

        /// <summary>
        /// 在更新数据前对数据的修改操作
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        protected override void OnBeforeUpdate(AutoDetails info)
        {
            info.LastModifyTime = DateTime.Now;
            info.LastModifyUserId = CurrentUser.Id;
            if (info.SortCode == null)
            {
                info.SortCode = 99;
            }
        }


        /// <summary>
        /// 根据推荐属性查询车辆信息
        /// </summary>
        /// <param name="rec">属性值，置顶值为Istop，推荐值为IsRed，热门值为IsHot；幻灯值为IsSlide</param>
        /// <param name="token">token令牌</param>
        /// <returns>所有置顶的品牌</returns>
        [HttpGet("FindCarListByRecommend")]
        public IActionResult FindCarListByRecommend(string rec, string token)
        {
            //检查用户是否有权限，否则抛出MyDenyAccessException异常
            CommonResult result = new CommonResult();
            result = CheckToken(token);
            if (result.ErrCode == ErrCode.successCode)
            {
               
                AutoDetailsApp autoDetailsApp = new AutoDetailsApp();
                List<AutoDetailsDto> list = autoDetailsApp.FindCarListByRecommend(rec);

                result.Success = true;
                result.ResData = list;
            }
            return ToJsonContent(result);
        }
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="info">请求参数</param>
        /// <param name="token">token令牌</param>
        /// <returns>指定对象的集合</returns>
        [HttpPost("FindWithPagerFront")]
        public IActionResult FindWithPagerFront(AutoDetailsSearchRequest info, string token)
        {
            //检查用户是否有权限，否则抛出MyDenyAccessException异常
            // CheckAuthorized(AuthorizeKey.ListKey);
            CommonResult result = new CommonResult();
            result = CheckToken(token);
            if (result.ErrCode == ErrCode.successCode)
            {
                string keywords = info.Keywords;
                string orderByDir = info.Order;
                string orderFlied = info.Sort;
                string where = GetPagerCondition();
                bool order = orderByDir == "asc" ? false : true;
                if (!string.IsNullOrEmpty(keywords))
                {
                    where += string.Format(" and (Title like '%{0}%' or SeoTitle like '%{0}%' or Tags like '%{0}%' or Zhaiyao like '%{0}%')", keywords);
                }
                if (!string.IsNullOrEmpty(info.AutoBrandId))
                {
                    where += string.Format(" and BrandId='{0}'",info.AutoBrandId);
                }
                if (!string.IsNullOrEmpty(info.MemberId))
                {
                    where += string.Format(" and BrandId='{0}'", info.MemberId);
                }
                string rec = info.Rec;
                if (rec == "IsTop")
                {
                    where += " and IsTop=1";
                }
                else if (rec == "IsRed")
                {
                    where += " and IsRed=1";
                }
                else if (rec == "IsHot")
                {
                    where += " and IsHot=1";
                }
                else if (rec == "IsSlide")
                {
                    where += " and IsSlide=1";
                }
                if (!string.IsNullOrEmpty(info.ShopId))
                {
                    ReferralsDto referrals = new ReferralsApp().GetById(info.ShopId);
                    if (referrals != null && !referrals.IsRepeled)
                    {
                        where += string.Format(" and MemberId='{0}' Or IsOpen=1", referrals.MemberId);
                    }
                }

                PagerInfo pagerInfo = new PagerInfo();
                pagerInfo.PageSize = info.PageSize;
                pagerInfo.CurrenetPageIndex = info.PageNo;
                AutoDetailsApp autoDetailsApp = new AutoDetailsApp();
                List<AutoDetailsDto> list = autoDetailsApp.FindWithPager(where, pagerInfo, orderFlied, order);
               
                result.ResData = new
                {
                    recordsTotal = pagerInfo.RecordCount,
                    recordsFiltered = pagerInfo.RecordCount,
                    data = list
                };
            }
            return ToJsonContent(result);
        }

        /// <summary>
        /// 根据主键Id获取一个对象信息
        /// </summary>
        /// <param name="id">主键Id</param>
        /// <param name="token">token令牌</param>
        /// <returns></returns>
        [HttpGet("GetById")]
        public override IActionResult GetById(string id, string token)
        {
            CommonResult result = new CommonResult();

            result = CheckToken(token);
            if (result.ErrCode == ErrCode.successCode)
            {
                AutoDetailsApp autoDetailsApp = new AutoDetailsApp();
                AutoDetailsDto autoDetailsDto = autoDetailsApp.GetById(id);
                if (autoDetailsDto != null)
                {
                    result.ResData = autoDetailsDto;
                }
                else
                {
                    result.ErrMsg = ErrCode.err60001;
                    result.ErrCode = "60001";
                }
            }
            return ToJsonContent(result);
        }
    }
}